<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/adminusers.php
#	FUNCTION:		Custom handler for the Admin Users page
#	AUTHOR:			Cameron Morrow
#	CREATED:		29/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# D A T A   F O R   U S E R S
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Table
$current_table = $PROJECT_DB_TABLES["users"];

# Field to sort browse on
$browse_sort_field = "u_id";

# Field to group browse on
$browse_group_field = "g_name";

# Is index numeric
$index_numeric = false;

# Index field
$index_field = "u_id";

# Type name
$type_name = "User";

# Plural
$type_name_plural = "Users";

# Custom Columns
$custom_columns = array();

# Change password
$custom_columns[] = array("Password", "\"<a href=\\\"javascript:openChangePasswordAdminWin('[u_id]');\\\">Change Password</a>\"");

# Actions
$custom_columns[] = generateAddEditColumn($index_field);

# Browse SQL
$browse_sql_additional_where_clauses = array($current_table .".u_group = " . $PROJECT_DB_TABLES["groups"] . ".g_id");

# Limit to groups this user level can access
$group_limits = "";
foreach ($USER -> getEditableGroups() as $group_counter => $editable_group) {
	 $group_limits .= "u_group = " . $editable_group;
	 if ($group_counter < count($USER -> getEditableGroups()) - 1) {
		 $group_limits .= " OR ";
	 }
}

# Add their own group
if ($group_limits == "") {
	$group_limits = "u_group = " . $USER -> getGroup();
} else {
	//$group_limits .= " OR u_group = " . $USER -> getGroup();
}


$browse_sql_additional_where_clauses[] = "(" . $group_limits . ")";

$browse_sql_search_fields = array("u_id", "u_fname", "u_lname");

# Add limits to view only logged in/non-logged in users
$include_logged_in_status_limits = true;

# Edit SQL
$edit_sql_additional_selects = array($PROJECT_DB_TABLES["groups"] . ".g_name", $PROJECT_DB_TABLES["groups"] . ".g_id");
$edit_sql_additional_tables = array($PROJECT_DB_TABLES["groups"]);
$edit_sql_additional_where_clauses = array($current_table .".u_group = " . $PROJECT_DB_TABLES["groups"] . ".g_id");

# Add form for editing group users
addContent("<h2>Manage Groups</h2>");
addContent("<form name=\"groupsform\" id=\"groupsform\" action=\"#\" method=\"get\">");

# Current groups
$current_groups_sql = "SELECT g_id, g_name FROM " . $PROJECT_DB_TABLES["groups"] . " WHERE ";
foreach ($USER -> getEditableGroups() as $group_counter => $editable_group) {
	 $current_groups_sql .= "g_id = " . $editable_group;
	 if ($group_counter < count($USER -> getEditableGroups()) - 1) {
		 $current_groups_sql .= " OR ";
	 }
}
$current_groups_sql .= " ORDER BY g_name ASC";


$current_groups = getRecord($current_groups_sql);


if ($current_groups && count($current_groups) > 0) {
	addContent("<p>Manage users for: ");
	$displayed_users_group = false;
	foreach ($current_groups as $current_group) {
		addContent("<input type=\"button\" onclick=\"editGroupUsers('" . $current_group["g_id"] . "');\" value=\"" . $current_group["g_name"] . "\" /> ");
		if ($current_group["g_id"] == $USER -> getGroup()) {
			$displayed_users_group = true;
		}
	}
	if (!$displayed_users_group) {
		addContent("<input type=\"button\" onclick=\"editGroupUsers('" . $USER -> getGroup() . "');\" value=\"" . $USER -> getGroupName() . "\" /> ");
	}
	addContent("</p></form>");
}



include("../includes/custom/adminhandler.php");

?>